package com.hxh.generater.springBoot.simple.gen;

import com.hxh.config.Constants;
import com.hxh.entity.Project;
import com.hxh.generater.springBoot.genBase.GenSystemBase;
import com.hxh.util.DirClone;
import com.hxh.util.GenUtil;
import org.apache.commons.lang3.StringUtils;

import java.io.*;

import static com.hxh.config.Constants.ProjectDirSuffix;

/**
 * @ProjectName: AICode
 * @Package: com.hxh.generater
 * @Author: HXH
 * @Description: 系统基础代码
 * @Date: 2023/11/28 15:08
 */
public class GenSimpleSystemBase implements GenSystemBase {

    @Override
    public void generationSystemBase(Project project) {
        generationApplicationClass(project);
        generationQueryBaseClass(project);
        generationApiResultClass(project);
        generationResultCodeClass(project);
        generationPom(project);
        generationReadme(project);
        generationAllYml(project);
        generationTableInfoService(project);
        try {
            generationHistoryAnnotationClass(project);
            generationHistoryAnnotationProcessorClass(project);
            generationApplicationContextUtil(project);
        } catch (Exception ignored) {
        }
    }


    private void generationApplicationClass(Project project) {
        try {
            File classFile = GenUtil.newFile(project, "application", "Application");
            FileWriter fileWriter = new FileWriter(classFile);
            BufferedWriter writer = new BufferedWriter(fileWriter);

            File template = new File(Constants.templatePathPrefix + "simple\\template\\" + "Application");
            BufferedReader br = new BufferedReader(new FileReader(template));
            String line;
            while ((line = br.readLine()) != null) {
                if (StringUtils.isNotBlank(line)) {
                    line = line.replaceAll("#GroupId#", project.getBackendProject().getProjectGroup());
                }
                if (StringUtils.isBlank(line)) {
                    writer.newLine();
                } else {
                    writer.write(line);
                    writer.newLine();
                }
            }
            br.close();
            writer.close();
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private void generationQueryBaseClass(Project project) {
        try {
            File classFile = GenUtil.newFile(project, "entity\\input", "BasePageQuery");
            FileWriter fileWriter = new FileWriter(classFile);
            BufferedWriter writer = new BufferedWriter(fileWriter);

            File template = new File(Constants.templatePathPrefix + "simple\\template\\" + "entity\\input\\BasePageQuery");
            BufferedReader br = new BufferedReader(new FileReader(template));
            String line;
            while ((line = br.readLine()) != null) {
                if (StringUtils.isNotBlank(line)) {
                    line = line.replaceAll("#GroupId#", project.getBackendProject().getProjectGroup());
                }
                if (StringUtils.isBlank(line)) {
                    writer.newLine();
                } else {
                    writer.write(line);
                    writer.newLine();
                }
            }
            br.close();

            writer.close();
            fileWriter.close();

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private void generationApiResultClass(Project project) {
        try {
            File classFile = GenUtil.newFile(project, "entity\\output", "ApiResult");
            FileWriter fileWriter = new FileWriter(classFile);
            BufferedWriter writer = new BufferedWriter(fileWriter);

            File template = new File(Constants.templatePathPrefix + "simple\\template\\" + "preClass\\ApiResult");
            BufferedReader br = new BufferedReader(new FileReader(template));
            String line;
            while ((line = br.readLine()) != null) {
                if (StringUtils.isNotBlank(line)) {
                    line = line.replaceAll("#GroupId#", project.getBackendProject().getProjectGroup());
                }
                if (StringUtils.isBlank(line)) {
                    writer.newLine();
                } else {
                    writer.write(line);
                    writer.newLine();
                }
            }
            br.close();

            writer.close();
            fileWriter.close();

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private void generationResultCodeClass(Project project) {
        try {
            File classFile = GenUtil.newFile(project, "entity\\enumeration", "ResultCode");
            FileWriter fileWriter = new FileWriter(classFile);
            BufferedWriter writer = new BufferedWriter(fileWriter);

            File template = new File(Constants.templatePathPrefix + "simple\\template\\" + "preClass\\ResultCode");
            BufferedReader br = new BufferedReader(new FileReader(template));
            String line;
            while ((line = br.readLine()) != null) {
                if (StringUtils.isNotBlank(line)) {
                    line = line.replaceAll("#GroupId#", project.getBackendProject().getProjectGroup());
                }
                if (StringUtils.isBlank(line)) {
                    writer.newLine();
                } else {
                    writer.write(line);
                    writer.newLine();
                }
            }
            br.close();

            writer.close();
            fileWriter.close();

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private void generationReadme(Project project) {
        String sourcePath = Constants.templatePathPrefix + "simple\\template\\readme.md";
        String destPath = project.getBackendProject().getProjectDir() + project.getBackendProject().getProjectName() + "\\readme.md";
        DirClone.fileInputOutput(sourcePath, destPath);

    }
    private void generationPom(Project project) {
        try {
            File pomFile = GenUtil.newFile(project, "pom", "pom");
            FileWriter fileWriter = new FileWriter(pomFile);
            BufferedWriter writer = new BufferedWriter(fileWriter);

            File template = new File(Constants.templatePathPrefix + "simple\\template\\" + "pom");
            BufferedReader br = new BufferedReader(new FileReader(template));
            String line;
            while ((line = br.readLine()) != null) {
                if (StringUtils.isNotBlank(line)) {
                    line = line.replaceAll("#GroupId#", project.getBackendProject().getProjectGroup());
                    line = line.replaceAll("#ArtifactId#", project.getBackendProject().getProjectName());
                }
                if (StringUtils.isBlank(line)) {
                    writer.newLine();
                } else {
                    writer.write(line);
                    writer.newLine();
                }
            }
            br.close();
            writer.close();
            fileWriter.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void generationTableInfoService(Project project) {
        try {
            File classFile = GenUtil.newFile(project, "service", "TableInfoService");
            FileWriter fileWriter = new FileWriter(classFile);
            BufferedWriter writer = new BufferedWriter(fileWriter);

            File template = new File(Constants.templatePathPrefix + "simple\\template\\" + "TableInfoService");
            BufferedReader br = new BufferedReader(new FileReader(template));
            String line;
            while ((line = br.readLine()) != null) {
                if (StringUtils.isNotBlank(line)) {
                    line = line.replaceAll("#GroupId#", project.getBackendProject().getProjectGroup());
                }
                if (StringUtils.isBlank(line)) {
                    writer.newLine();
                } else {
                    writer.write(line);
                    writer.newLine();
                }
            }
            br.close();

            writer.close();
            fileWriter.close();

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private void generationAllYml(Project project) {
        generationApplicationYml(project);
        generationApplicationDBYml(project);
        generationApplicationMybatisYml(project);
        generationApplicationServiceYml(project);

    }

    private void generationApplicationYml(Project project) {
        try {
            File classFile = GenUtil.newFile(project, "applicationConfig", "application");
            FileWriter fileWriter = new FileWriter(classFile);
            BufferedWriter writer = new BufferedWriter(fileWriter);

            File template = new File(Constants.templatePathPrefix + "simple\\template\\" + "resources\\application");
            BufferedReader br = new BufferedReader(new FileReader(template));
            String line;
            while ((line = br.readLine()) != null) {
                if (StringUtils.isNotBlank(line)) {
                    line = line.replace("#projectPort#", project.getBackendProject().getProjectPort());
                    line = line.replace("#projectName#", project.getBackendProject().getProjectName());
                    writer.write(line);
                }
                writer.newLine();
            }
            br.close();
            writer.close();
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void generationApplicationDBYml(Project project) {
        try {
            File classFile = GenUtil.newFile(project, "applicationConfig", "application-db");
            FileWriter fileWriter = new FileWriter(classFile);
            BufferedWriter writer = new BufferedWriter(fileWriter);

            File template = new File(Constants.templatePathPrefix + "simple\\template\\" + "resources\\application-db");
            BufferedReader br = new BufferedReader(new FileReader(template));
            String line;
            while ((line = br.readLine()) != null) {
                if (StringUtils.isNotBlank(line)) {
                    line = line.replace("#MYSQL_CONNET_URL#", project.getProjectDatasource().getMysqlConnectUrl());
                    line = line.replace("#MYSQL_USERNAME#", project.getProjectDatasource().getMysqlUsername());
                    line = line.replace("#MYSQL_PASSWORD#", project.getProjectDatasource().getMysqlPassword());
                    line = line.replace("#MYSQL_DRIVER_CJ#", project.getProjectDatasource().getMysqlDriver());
                    writer.write(line);
                }
                writer.newLine();
            }
            br.close();
            writer.close();
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void generationApplicationMybatisYml(Project project) {
        try {
            File classFile = GenUtil.newFile(project, "applicationConfig", "application-mybatis");
            FileWriter fileWriter = new FileWriter(classFile);
            BufferedWriter writer = new BufferedWriter(fileWriter);

            File template = new File(Constants.templatePathPrefix + "simple\\template\\" + "resources\\application-mybatis");
            BufferedReader br = new BufferedReader(new FileReader(template));
            String line;
            while ((line = br.readLine()) != null) {
                if (StringUtils.isNotBlank(line)) {
                    writer.write(line);
                }
                writer.newLine();
            }
            br.close();
            writer.close();
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void generationApplicationServiceYml(Project project) {
        try {
            File classFile = GenUtil.newFile(project, "applicationConfig", "application-service");
            FileWriter fileWriter = new FileWriter(classFile);
            BufferedWriter writer = new BufferedWriter(fileWriter);

            File template = new File(Constants.templatePathPrefix + "simple\\template\\" + "resources\\application-service");
            BufferedReader br = new BufferedReader(new FileReader(template));
            String line;
            String resourcesPre = ProjectDirSuffix.substring(0, ProjectDirSuffix.lastIndexOf("\\"));
            String uploadfilePath = project.getBackendProject().getProjectDir() + project.getBackendProject().getProjectName() + "\\" + resourcesPre + "\\resources\\sysUpload\\";
            while ((line = br.readLine()) != null) {
                if (StringUtils.isNotBlank(line)) {
                    line = line.replace("#uploadfilePath#", uploadfilePath);
                    writer.write(line);
                }
                writer.newLine();
            }
            br.close();
            writer.close();
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void generationHistoryAnnotationClass(Project project) throws IOException {
        File classFile = GenUtil.newFile(project, "annotation", "HistoryAnnotation");
        File template = new File(Constants.templatePathPrefix + "simple\\template\\" + "annotation\\HistoryAnnotation");
        try (FileWriter fileWriter = new FileWriter(classFile);
             BufferedWriter writer = new BufferedWriter(fileWriter);
             BufferedReader br = new BufferedReader(new FileReader(template))) {
            String line;
            while ((line = br.readLine()) != null) {
                if (StringUtils.isNotBlank(line)) {
                    line = line.replaceAll("#GroupId#", project.getBackendProject().getProjectGroup());
                }
                if (StringUtils.isBlank(line)) {
                    writer.newLine();
                } else {
                    writer.write(line);
                    writer.newLine();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void generationHistoryAnnotationProcessorClass(Project project) throws IOException {
        File classFile = GenUtil.newFile(project, "annotation", "HistoryAnnotationProcessor");
        File template = new File(Constants.templatePathPrefix + "simple\\template\\" + "annotation\\HistoryAnnotationProcessor");
        try (FileWriter fileWriter = new FileWriter(classFile);
             BufferedWriter writer = new BufferedWriter(fileWriter);
             BufferedReader br = new BufferedReader(new FileReader(template))) {
            String line;
            while ((line = br.readLine()) != null) {
                if (StringUtils.isNotBlank(line)) {
                    line = line.replaceAll("#GroupId#", project.getBackendProject().getProjectGroup());
                }
                if (StringUtils.isBlank(line)) {
                    writer.newLine();
                } else {
                    writer.write(line);
                    writer.newLine();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void generationApplicationContextUtil(Project project) throws IOException {
        File classFile = GenUtil.newFile(project, "config", "ApplicationContextUtil");
        File template = new File(Constants.templatePathPrefix + "simple\\template\\" + "config\\ApplicationContextUtil");
        try (FileWriter fileWriter = new FileWriter(classFile);
             BufferedWriter writer = new BufferedWriter(fileWriter);
             BufferedReader br = new BufferedReader(new FileReader(template))) {
            String line;
            while ((line = br.readLine()) != null) {
                if (StringUtils.isNotBlank(line)) {
                    line = line.replaceAll("#GroupId#", project.getBackendProject().getProjectGroup());
                }
                if (StringUtils.isBlank(line)) {
                    writer.newLine();
                } else {
                    writer.write(line);
                    writer.newLine();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
